local KillListView = class("KillListView",function()
    return display.newNode()
end)

function KillListView:ctor()
    --ui root
--    self.mUINode = cc.CSLoader:createNode(Res.BattleKillListNode, false, true)
    self.mUINode = MainBattleUICache:find(Res.BattleKillListNode)
    -- self.mUINode:setContentSize(cc.size(display.width,display.height))
    display.doLayout(self.mUINode)
    self:addChild(self.mUINode)
    
    
    self.selectedBtn = self.mUINode:findChild("Panel/selected_btn")
    assert(self.selectedBtn)

    self.killNpcIcons = {}
    self.killAniNodes = {}
    for i=1,3 do
        local panelStr = "Panel/Panel_equip/Panel_"..i
        local iconNode = self.mUINode:findChild(panelStr.."/icon")
        assert(iconNode)
        iconNode:setVisible(false)
        table.insert(self.killNpcIcons, iconNode)

        local animNode = self.mUINode:findChild(panelStr.."/ani")
        assert(animNode)
        table.insert(self.killAniNodes, animNode)
        
        local action = cc.CSLoader:createTimeline(Res.BattleThreeKillNode)
        if action then
            animNode:runAction(action)
            action:gotoFrameAndPlay(0, action:getDuration(), true)
        end
    end

    --设置事件
    self:init()

    self:refreshKillList(nil, false)
end

function KillListView:init()
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_REFRESH_KILLLIST,handler(self,KillListView.refreshKillList))

    self:initUIAction()

    self:setSetup()
end

function KillListView:initUIAction()
    if self.mAction == nil then
        self.mAction = cc.CSLoader:createTimeline(Res.BattleKillListNode)
        if self.mAction then
            self:runAction(self.mAction)
            self.mAction:gotoFrameAndPause(0)

            self:playShowAction()
        end
    end
end

function KillListView:playShowAction()
    self.isHiding = false
    local uiAction = self.mAction
	if uiAction and (not tolua.isnull(uiAction)) then
		uiAction:clearLastFrameCallFunc()

    	uiAction:play("in",false)
    	uiAction:setLastFrameCallFunc(function()
			-- self.isHiding = false
            if not tolua.isnull(uiAction) then
	            uiAction:clearLastFrameCallFunc()
            end
	    end)
	end
end

function KillListView:playHideAction()
	self.isHiding = true
    local uiAction = self.mAction
	if uiAction and (not tolua.isnull(uiAction)) then
		uiAction:clearLastFrameCallFunc()

    	uiAction:play("out",false)
    	uiAction:setLastFrameCallFunc(function()
			-- self.isHiding = false
            if not tolua.isnull(uiAction) then
	            uiAction:clearLastFrameCallFunc()
            end
	    end)
	end
end


function KillListView:setSetup()
    --touchEvent
    local function touchEvent_showKillList(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.isHiding then
                self:playShowAction()
            else
                self:playHideAction()
            end
        end
    end
    self.selectedBtn:addTouchEventListener(touchEvent_showKillList)
end

function KillListView:refreshKillList(iconPaths, isThreeKill)
    local action = self:getActionByTag(Enums.CommonActionTag.BattleLionThreeKillUIHide)
    if isThreeKill then
        if action then
            --do nothing
        else
            action = cca.seq({cca.delay(3), cca.hide()})
            action:setTag(Enums.CommonActionTag.BattleLionThreeKillUIHide)
            self:runAction(action)
        end
    else
        if action then
            self:stopActionByTag(Enums.CommonActionTag.BattleLionThreeKillUIHide)
            self:setVisible(true)
        else
            --do nothing
        end
    end

    for k,v in pairs(self.killAniNodes) do
        v:setVisible(isThreeKill)
    end

    for k,v in pairs(self.killNpcIcons) do
        local iconPath = nil
        if iconPaths then
            iconPath = iconPaths[k]
        end
        
        if iconPath then
            v:loadTexture(iconPath)
            v:setVisible(true)
        else
            v:setVisible(false)
        end
    end
end



return KillListView